package com.theanh.RssActivity.ui;

import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.preference.ListPreference;
import android.preference.CheckBoxPreference;
import com.theanh.RssActivity.Store;
import com.theanh.RssActivity.db.DbAdapter;
import com.theanh.RssActivity.util.DbUtils;

public class SettingScreen extends PreferenceActivity {
	private static final String TAG = "SettingScreen";
	private CheckBoxPreference autoUpdate;
	private CheckBoxPreference displayImageFeed;
	private CheckBoxPreference hideReadedFeed;
	private ListPreference numberFeedPage;
	private ListPreference timerKeepFeed;
	private CheckBoxPreference autoCheckUrl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(com.theanh.RssActivity.R.xml.preferences_setting);
		autoUpdate = (CheckBoxPreference) findPreference("prefAutoUpdate");
		autoUpdate.setOnPreferenceClickListener(autoUpdate_onClick);
		autoUpdate.setChecked(Store.setting.isAutoUpdate());
		// set CheckboxPreference Load image on Feed
		displayImageFeed = (CheckBoxPreference) findPreference("prefDisplayImageFeed");
		displayImageFeed
				.setOnPreferenceClickListener(displayImageFeed_onClick);
		displayImageFeed.setChecked(Store.setting.isDisplayImage());
		// set CheckboxPreference Hide Read Feed
		hideReadedFeed = (CheckBoxPreference) findPreference("prefHideReadFeed");
		hideReadedFeed
				.setOnPreferenceClickListener(hideImageFeed_onClick);
		hideReadedFeed.setChecked(Store.setting.isHideReaded());
		// set ListPreference Number Feed on page
		numberFeedPage = (ListPreference) findPreference("prefNumberFeedOnPage");
		numberFeedPage
				.setOnPreferenceChangeListener(numberFeedOnPage_onClick);
		
		// set ListPreference time delete Feed
		timerKeepFeed = (ListPreference) findPreference("prefKeepFeed");
		timerKeepFeed.setOnPreferenceChangeListener(keepFeed_onClick);
		
		autoCheckUrl = (CheckBoxPreference)findPreference("prefCheckValidateUrl");
		autoCheckUrl.setOnPreferenceClickListener(autoCheckUrl_onClick);
		autoCheckUrl.setChecked(Store.setting.isAutoCheckUrl());
	}

	Preference.OnPreferenceClickListener autoUpdate_onClick = new Preference.OnPreferenceClickListener() {
		public boolean onPreferenceClick(Preference pref) {
			Store.setting.setAutoUpdate(autoUpdate.isChecked());
			Log.i(TAG, "setAutoUpdate=" + Store.setting.isAutoUpdate());
			return true;
		}
	};
	// set onPreferenceClick of CheckboxPreference Load image on Feed
	Preference.OnPreferenceClickListener displayImageFeed_onClick = new Preference.OnPreferenceClickListener() {
		public boolean onPreferenceClick(Preference pref) {
			Store.setting.setDisplayImage(displayImageFeed.isChecked());
			Log.i(TAG, "isDisplayImage=" + Store.setting.isDisplayImage());
			return true;
		}
	};

	// set onPreferenceClick of CheckboxPreference Hide read Feed
	Preference.OnPreferenceClickListener hideImageFeed_onClick = new Preference.OnPreferenceClickListener() {
		public boolean onPreferenceClick(Preference pref) {
			Store.setting.setHideReaded(hideReadedFeed.isChecked());
			Log.i(TAG, "setHideReaded=" + Store.setting.isHideReaded());
			return true;
		}
	};
	// set onPreferenceClick of ListPreference number Feed on page
	Preference.OnPreferenceChangeListener numberFeedOnPage_onClick = new Preference.OnPreferenceChangeListener() {
		public boolean onPreferenceChange(Preference pref, Object obj) {
			try {
				int numberFeedOnPage = Integer.parseInt(obj.toString());
				Log.i(TAG, "numberFeedPage = " + numberFeedOnPage);
				Store.setting.setPageSize(numberFeedOnPage);
			} catch (Exception ex) {
				Log.e(TAG, ex.toString());
			}
			return true;
		}
	};
	// set onPreferenceClick of ListPreference Time delete Feed
	Preference.OnPreferenceChangeListener keepFeed_onClick = new Preference.OnPreferenceChangeListener() {
		public boolean onPreferenceChange(Preference pref, Object obj) {
			try {
				int timerKeepFeed = Integer.parseInt(obj.toString());
				Log.i(TAG, "timerKeepFeed = " + timerKeepFeed);
				Store.setting.setItemExpired(timerKeepFeed);
			} catch (Exception ex) {
				Log.e(TAG, ex.toString());
			}
			return true;
		}
	};
	//
	Preference.OnPreferenceClickListener autoCheckUrl_onClick = new Preference.OnPreferenceClickListener(){
		public boolean onPreferenceClick(Preference pref){
			Store.setting.setAutoCheckUrl(autoCheckUrl.isChecked());
			return true;
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.i(TAG,"onDestroy setting isAutoUpdate= "+Store.setting.isAutoUpdate());
		DbAdapter dbAdapter = null;
		try {
			dbAdapter = DbUtils.createAndOpenDbAdapter();
			dbAdapter.updateSetting(Store.setting);
		} catch (Exception ex) {
			Log.e(TAG, ex.toString());
		} finally {
			if (dbAdapter != null) {
				DbUtils.closeDbAdapter(dbAdapter);
			}
		}

		super.onDestroy();

	}

}
